package com.travel.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.commons.utils.Constant;
import com.travel.modules.asset.dao.GoodsDao;
import com.travel.modules.asset.dao.HotelDao;
import com.travel.modules.asset.dao.RouteDao;
import com.travel.modules.asset.dao.ScenicAreaDao;
import com.travel.modules.asset.entity.Goods;
import com.travel.modules.asset.entity.Hotel;
import com.travel.modules.asset.entity.Route;
import com.travel.modules.asset.entity.ScenicArea;
import com.travel.modules.asset.service.GoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    @Resource
    private ScenicAreaDao scenicAreaDao;

    @Resource
    private RouteDao routeDao;

    @Resource
    private HotelDao hotelDao;

    @Override
    public Object getGoodsObject(String goodsId) {
        if (StringUtils.isBlank(goodsId)) {
            return null;
        }
        String tableName = goodsDao.selectOne(new QueryWrapper<Goods>().eq("goods_id", goodsId)).getTableName();
        switch (tableName) {
            case Constant.T_SCENIC_AREA: return scenicAreaDao.selectOne(new QueryWrapper<ScenicArea>().eq("area_id", goodsId));
            case Constant.T_ROUTE: return routeDao.selectOne(new QueryWrapper<Route>().eq("goods_id", goodsId));
            case Constant.T_HOTEL: return hotelDao.selectOne(new QueryWrapper<Hotel>().eq("hotel_id", goodsId));
            default: return new Object();
        }
    }

    @Override
    public void addReputation(Object goodsObj, String goodsId) {
        if (goodsId.contains("h-")) {
            Hotel hotel = (Hotel) goodsObj;
            hotel.setReputation(hotel.getReputation() + 1);
            hotelDao.updateById(hotel);
        } else if (goodsId.contains("r-")) {
            Route route = (Route) goodsObj;
            route.setReputation(route.getReputation() + 1);
            routeDao.updateById(route);
        }
    }

    @Override
    public void addHot(Object goodsObj, String goodsId) {
        if (goodsId.contains("h-")) {
            Hotel hotel = (Hotel) goodsObj;
            hotel.setHot(hotel.getHot() + 1);
            hotelDao.updateById(hotel);
        } else if (goodsId.contains("r-")) {
            Route route = (Route) goodsObj;
            route.setHot(route.getHot() + 1);
            routeDao.updateById(route);
        }
    }
}
